<?php

/**
 * oRs
 *
 * @version 3.0
 * @copyright 2009 Ron Planken
 * @class: menu
 * @description: Handles menu actions
 */

class menu {

	private $_system;
	
	function __construct($system) {
		$this->_system = $system;
	}

	function get($menu_category_id = 0) {

		$cat_id = 0;
		
		$menu_category_id = $this->_system->_db->secure_int($menu_category_id);
		
		if ($menu_category_id == 0) {
			$query_categories = "SELECT	 menu_category_id
										,menu_category_order  	 
										,menu_category_text
									FROM menu_category
								ORDER BY menu_category_order
										 ";
			
			$query ="SELECT menu_category_id
						   ,menu_order
						   ,menu_page
						   ,menu_action
						   ,menu_text
						   ,menu_level
						   ,menu_external
					   FROM menu
					  WHERE menu_category_id = {cat_id}
						AND menu_visible = 1
				   ORDER BY menu_order ASC
							";
		} else {
			$query_categories = "SELECT	 menu_category_id
										,menu_category_order  	 
										,menu_category_text
									FROM menu_category
					  			   WHERE menu_category_id = $menu_category_id
								ORDER BY menu_category_order
										 ";

			$query ="SELECT menu_category_id
						   ,menu_order
						   ,menu_page
						   ,menu_action
						   ,menu_text
						   ,menu_level
						   ,menu_external
					   FROM menu
					  WHERE menu_category_id = $menu_category_id
					  	AND menu_visible = 1
				   ORDER BY menu_order ASC
							";
		}
		
		$menu_categories = $this->_system->_db->selectMultiQuery($query_categories);
		
		foreach ($menu_categories as $category) {
			$cat_id = $category['menu_category_id'];
		

			
			$query = str_replace("{cat_id}", (string)$cat_id, $query);

			$menuItems[$cat_id]['items'] = $this->_system->_db->selectMultiQuery($query);
			
			$menuItems[$cat_id]['text'] = $category['menu_category_text'];
						

		}
		
		return $menuItems;
	}
	
	
	function getAction($page) {
		
	  $page = $this->_system->_db->secure_string($page);
	
		$query ="SELECT menu_page
				       FROM menu
				      WHERE menu_action = $page
			     ORDER BY menu_order ASC
						";
		
		$page = $this->_system->_db->selectQuery($query);
		
		return $page;
	}

	function getLevel($page) {
		
		$page = $this->_system->_db->secure_string($page);
	
		$query ="SELECT menu_level
				       FROM menu
				      WHERE menu_page = $page
						";
		
		$level = $this->_system->_db->selectQuery($query);
		
		return $level;
	}
		
	//TODO: Write the save function
	function save() {
		
		return $status;
	}
}
?>